<?php
class AclController extends Exts_Controller_SecureAction
{
    public function init()
    {
        parent::init();
        if( ! $this->_hasParam('role')) {
            return $this->_return('/role/list/');
        }
        $this->view->roleId = $this->_getParam('role');
    }

    public function listAction()
    {
        $rulesModel = new Exts_Models_Rules();
        if( $this->getRequest()->isPost() ) {

            $params = $this->_getAllParams();
            $permission = $params['permission'];
            $resourcesModel = new Exts_Models_Resources();
            $bind = array();
            foreach( $permission as $k => $v  ) {
                $per = explode('-', $k );
                $bind[$k]['resource_id'] = $resourcesModel->getIdByName( $per[0] );
                $bind[$k]['operation'] = $per[1];
                $bind[$k]['role_id'] = $this->_getParam('role');
            }

            $rulesModel->saveBatch( $bind, $this->_getParam('role') );

        }
        
        $results = $rulesModel->getRulesByRoleId( $this->_getParam('role') );
        $rules = array();
        foreach( $results as $r ) {
            $rules[$r['name']][] = $r['operation'];
        }



        $this->view->have = $rules;

        $aclModel = new Exts_Models_Acl();  
        $acl2 = $aclModel->getResources( APPLICATION_PATH . '/controllers/' );
        $acl1 = $aclModel->getResources( APPLICATION_PATH . '/../library/Exts/Controller/' );
        if( is_array( $acl1 ) && is_array($acl2 )) {
            $acl = array_merge($acl1, $acl2 );
            $this->view->permissions =  $acl;
        } else{
            $this->view->permissions =  array();
        }
        //$this->view->paginator = new Exts_Paginator( $query, $this->_page, $this->view );
    }

}
